package edu.neu.cloud_factory.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import edu.neu.cloud_factory.entity.po.DeviceType;
import edu.neu.cloud_factory.dao.DeviceTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.neu.cloud_factory.entity.vo.DeviceTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备类型 服务实现类
 * </p>
 *
 * @author halozhy
 * @since 2021-07-15
 */
@Service
public class DeviceTypeService extends ServiceImpl<DeviceTypeMapper, DeviceType> implements IService<DeviceType> {

    @Autowired
    DeviceTypeMapper deviceTypeMapper;

    public JSONObject listDeviceType(int page, int limit, String id, String name) {
        QueryWrapper<DeviceType> queryWrapper = new QueryWrapper<>();
        if (id != null && !id.isEmpty()) {
            queryWrapper.like("dt_id", id);
        }
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("dt_name", name);
        }

        List<DeviceType> deviceTypeList = deviceTypeMapper.listPage((page - 1) * limit, limit, queryWrapper);
        List<DeviceTypeVo> deviceTypeVoList = new ArrayList<>();
        deviceTypeList.forEach(deviceType -> {
            DeviceTypeVo deviceTypeVo = new DeviceTypeVo();
            deviceTypeVo.setId(deviceType.getDtId());
            deviceTypeVo.setName(deviceType.getDtName());
            deviceTypeVoList.add(deviceTypeVo);
        });
        JSONObject result = new JSONObject();
        result.put("data", deviceTypeVoList);
        result.put("count", deviceTypeMapper.selectCount(queryWrapper));
        return result;
    }
}
